//
//  LYDropDownView.m
//  SelectCity
//
//  Created by liuyan on 2019/3/20.
//  Copyright © 2019年 liuyan. All rights reserved.
//

#import "LYDropDownView.h"
#import "LYDropButton.h"

@interface LYDropDownView()

@property (nonatomic, strong) NSArray *titleArray;
@property (nonatomic, strong) UIColor *normalColor;
@property (nonatomic, strong) UIColor *selectColor;
@property (nonatomic, copy) NSString *normalImage;
@property (nonatomic, copy) NSString *selectImage;
@property (nonatomic, strong) NSArray<UIView *> *dropViews;

//当前点击按钮
@property (nonatomic, strong) LYDropButton *currentDropBtn;

@end

@implementation LYDropDownView

- (instancetype)initWithFrame:(CGRect)frame titleArray:(NSArray *)array normalColor:(UIColor *)normalColor selectColor:(UIColor *)selectColor normalImage:(NSString *)normalImage selectImage:(NSString *)selectImage dropViews:(NSArray<UIView *>*)views{
    self = [super initWithFrame:frame];
    if (self) {
        _titleArray = array;
        _normalColor = normalColor;
        _selectColor = selectColor;
        _normalImage = normalImage;
        _selectImage = selectImage;
        _dropViews = views;
        
        self.currentIndex = 0;
        
        [self createSubview];
    }
    return self;
}

- (void)createSubview {
    self.backgroundColor = [UIColor whiteColor];
    CGFloat dropwidth = (WIDTH - self.titleArray.count+1)/self.titleArray.count;
    self.dropBackview = [[UIView alloc] initWithFrame:CGRectMake(0,0, self.width, 40)];
    [self addSubview:self.dropBackview];
    [self.dropBackview borderColor:THEMECOLOR_OF_TEXT_cccccc borderWidth:lineH borderType:BorderSideTypeBottom];
    
    for (NSInteger i = 0 ; i < self.titleArray.count; i++ ) {
        LYDropButton *button = [[LYDropButton alloc] init];
        button.frame = CGRectMake((dropwidth+1)*i, 0, dropwidth, 40);
        [button setTitle:self.titleArray[i] forState:UIControlStateNormal];
        [button setTitleColor:self.normalColor forState:UIControlStateNormal];
        [button setTitleColor:self.selectColor forState:UIControlStateSelected];
        [button setImage:[UIImage imageNamed:self.normalImage] forState:UIControlStateNormal];
        [button setImage:[UIImage imageNamed:self.selectImage] forState:UIControlStateSelected];
        button.titleLabel.font = [UIFont systemFontOfSize:15];
        button.tag = 1000+i;
        [button addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
        [self.dropBackview addSubview:button];
        
        if (i > 0) {
            UIView *line = [[UIView alloc] initWithFrame:CGRectMake(i*dropwidth, self.dropBackview.height/6, 1, self.dropBackview.height*2/3)];
            line.backgroundColor = RGB(247, 247, 247);
            [self.dropBackview addSubview:line];
        }
    }
}

- (void)buttonClick:(LYDropButton *)button {
    if (self.delegate && [self.delegate respondsToSelector:@selector(dropDownViewTapBack)]) {
        [self.delegate dropDownViewTapBack];
    }
     button.selected = !button.selected;
    if (self.currentDropBtn && self.currentDropBtn != button && self.currentDropBtn.selected == YES) {
        self.currentDropBtn.selected = NO;
        NSInteger lastIndex = self.currentDropBtn.tag - 1000;
        UIView *lastview = self.dropViews[lastIndex];
        [self btnClick:self.currentDropBtn conditionView:lastview];
    }
    
    self.currentDropBtn = button;
    NSInteger index = button.tag - 1000;
    self.currentIndex = index;
    UIView *view = self.dropViews[index];
    [self btnClick:button conditionView:view];
}
- (void)btnClick:(LYDropButton *)btn conditionView:(UIView *)conditionView {
    if (btn.selected) {
        LYDropView *dropView = [LYDropView popSideContentView:conditionView belowView:self];
        dropView.backgroundColor = RGBA(0, 0, 0, 0.2);
        [dropView setWillRemovedFromeSuperView:^{
            
        }];
    } else {
        [LYDropView hidenPopView];
    }
}

- (void)setHeaderView:(UIView *)headerView{
    [self addSubview:headerView];
    self.dropBackview.top = headerView.bottom;
    self.height = self.dropBackview.bottom;
}

- (void)hiddenDropDownView {
    [LYDropView hidenPopView];
}
- (void)hiddenDropDownViewIndex:(NSInteger)index {
    LYDropButton *button = [self viewWithTag:1000+index];
    [self performSelector:@selector(buttonClick:) withObject:button];
}

@end
